import { useEffect, useState } from "react";
import { getBreadcRumb, push } from "@/utils/util";
import ListForm from '@/components/ListForm'
import { useLocation, useParams } from "react-router";
import { save, update, detail } from "@/apis/boss";
import { message } from "antd";

function BosstManageCreate() {
    const params = useParams()
    const location = useLocation()
    const [values, setvalues] = useState<any>()
    const text = location.pathname.search(/edit/) != -1 ? "编辑" : params.id ? '查看' : '新增'
    getBreadcRumb([{ name: "老板列表", path: "/bossList" }, { name: text + '老板' }])
    const list: listItem[] = [
        {
            name: "bossName",
            label: "老板名称",
            type: 'input',
            rules: [{ required: true, message: '请输入老板名称' }],
            disabled: text == '查看'
        },
        {
            name: "amount",
            label: "存入金额",
            type: 'input',
            rules: [{ required: true, message: "请输入老板坐标" }],
            disabled: text != '新增'
        },
    ]
    //确定调接口
    async function onFinish(values: bossAddParams) {
        if (text == '新增') {
            await save(values)
            message.success('新增老板成功')
        }
        if (text == '编辑') {
            if (params.id) {
                await update({ ...values, bossId: params.id })
                message.success('编辑老板成功')
                push('/bosslist')
            } else {
                message.error('编辑老板失败')
                return
            }
        }
        push('/bosslist')
    }
    async function init() {
        if (params.id) {
            let res = await detail({ bossId: params.id })
            setvalues({ ...res.data })
        }
    }
    useEffect(() => {
        init()
    }, [])
    return (
        <div className='tableWrap'>
            <h2>{text}老板</h2>
            <ListForm list={list} values={values} onFinish={onFinish}></ListForm>
        </div>
    )
}

export default BosstManageCreate;